package pers.kw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import pers.kw.config.oauth.context.MyParamValue;
import pers.kw.config.oauth.context.MyParamValueThreadLocal;
import pers.kw.contants.AuthParamName;
import pers.kw.entity.TokenDto;
import pers.kw.service.UserDetailStrategy;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class TestUserDetailStrategyService implements UserDetailStrategy {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public User getUserInfoByMobile(String mobile, List<GrantedAuthority> authorities) {
        return new User(mobile, passwordEncoder.encode(mobile), authorities);
    }

    @Override
    public Map<String, Object> initTokenInfo(MyParamValue paramValue) {
        MyParamValue myParamValue = MyParamValueThreadLocal.getCurrent();
        String userNo = myParamValue.getAuthParameter("username");
        String userType = myParamValue.getAuthParameter(AuthParamName.USER_TYPE);
        TokenDto tokenDTO = new TokenDto();
        tokenDTO.setUserNo(userNo);
        tokenDTO.setUserName(userNo);
        tokenDTO.setUserType(userType);
        tokenDTO.setMobile(userNo);
        return JSONObject.parseObject(JSONObject.toJSONString(tokenDTO),
                new TypeReference<Map<String, Object>>() {
                });
    }
}
